<template>
  <div>
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键字搜索">
        <el-input v-model="searchForm.content" placeholder="电影名/导演/概括/主演"></el-input>
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="searchForm.tid">
          <el-option v-for=" t in types" :key="t.id" :value="t.id" :label="t.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上映时间">
        <el-date-picker
          v-model="value1"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="add" style="float: left;margin-left: 50px">新增</el-button>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id" width="55"></el-table-column>
      <el-table-column prop="name" label="电影名" width="120">
        <template v-slot="scope">
          <span v-html="scope.row.name"></span>
        </template>
      </el-table-column>
      <el-table-column prop="dname" label="导演" width="120">
        <template v-slot="scope">
          <span v-html="scope.row.dname"></span>
        </template>
      </el-table-column>
      <el-table-column prop="aname" label="主演" width="120">
        <template v-slot="scope">
          <span v-html="scope.row.aname"></span>
        </template>
      </el-table-column>
      <el-table-column prop="contents" label="内容概括" width="200">
        <template v-slot="scope">
          <span v-html="scope.row.contents"></span>
        </template>
      </el-table-column>
      <el-table-column prop="tname" label="分类" width="60"></el-table-column>
      <el-table-column prop="created" label="上映时间" width="120"></el-table-column>
      <el-table-column prop="picture" label="宣传图">
        <template v-slot="scope">
          <img :src="scope.row.picture" width="80px" height="50px">
        </template>
      </el-table-column>
      <el-table-column label="操作" width="220">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="sizes"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <el-dialog title="编辑页面" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="电影名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="内容概括" prop="contents">
          <el-input type="textarea" v-model="form.contents"></el-input>
        </el-form-item>
        <el-form-item label="上映时间" prop="created">
          <el-date-picker v-model="form.created"></el-date-picker>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="form.tid">
            <el-option v-for=" t in types" :key="t.id" :value="t.id" :label="t.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="导演">
          <el-checkbox-group v-model="didss">
            <el-checkbox v-for="d in directs" :key="d.id" :label="d.id">{{ d.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="主演">
          <el-checkbox-group v-model="aidss">
            <el-checkbox v-for="a in actors" :key="a.id" :label="a.id">{{ a.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="宣传图">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:82/api/movie/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess">
            <img v-if="form.picture" :src="form.picture" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')">保存</el-button>
        <el-button @click="resetForm('form')">重置</el-button>
        <el-button @click="dialogFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ListView',
  data () {
    return {
      value1: [],
      tableData: [],
      selected: [],
      current: 1,
      size: 3,
      sizes: [3, 5],
      total: 0,
      searchForm: {
        content: '',
        tid: null
      },
      dialogFormVisible: false,
      form: {},
      types: [],
      directs: [],
      actors: [],
      didss: [],
      aidss: []
    }
  },
  created () {
    this.initData()
    this.listType()
    this.listDirect()
    this.listActor()
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.directIds = this.didss
          this.form.actorIds = this.aidss
          this.axios.post('/movie/save', this.form).then(res => {
            if (res.data.code === 200) {
              this.$message.success('编辑成功')
              this.dialogFormVisible = false
              this.initData()
            } else if (res.data.code === 201) {
              this.$message.error('姓名不能为空')
            } else if (res.data.code === 202) {
              this.$message.error('不能重复提交')
            }
          })
        } else {
          this.$message.error('信息不完整')
          return false
        }
      })
    },
    // 修改回显数据
    handleEdit (index, row) {
      this.dialogFormVisible = true
      this.form = JSON.parse(JSON.stringify(row))
      if (row.dids != null) {
        this.didss = row.dids.split(',').map(x => {
          return parseInt(x)
        })
      } else {
        this.didss = []
      }
      if (row.aids != null) {
        this.aidss = row.aids.split(',').map(x => {
          return parseInt(x)
        })
      } else {
        this.aidss = []
      }
    },
    // 删除
    handleDelete (index, row) {
      this.axios.get('/movie/delete', { params: { id: row.id } }).then(res => {
        this.$message.success('删除成功')
        this.initData()
      })
    },
    // 添加
    add () {
      this.$router.push('/add')
    },
    // 列表
    initData () {
      this.axios.get('/movie/list', {
        params: {
          page: this.current,
          size: this.size,
          content: this.searchForm.content,
          tid: this.searchForm.tid,
          startTime: this.value1 === '' ? '' : this.value1[0],
          endTime: this.value1 === '' ? '' : this.value1[1]
        }
      }).then(res => {
        this.tableData = res.data.content
        this.total = res.data.totalElements
      })
    },
    // 查询
    search () {
      this.current = 1
      this.initData()
    },
    // 选中行数据
    handleSelectionChange (val) {
      this.selected = val
    },
    // 图片上传
    handleAvatarSuccess (res, file) {
      this.form.picture = res.url
    },
    // 类型数据
    listType () {
      this.axios.get('/type/typeList').then(res => {
        this.types = res.data
      })
    },
    // 导演数据
    listDirect () {
      this.axios.get('/direct/list').then(res => {
        this.directs = res.data
      })
    },
    // 主演数据
    listActor () {
      this.axios.get('/actor/list').then(res => {
        this.actors = res.data
      })
    },
    // 重置
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    handleSizeChange (val) {
      this.size = val
      this.initData()
    },
    handleCurrentChange (val) {
      this.current = val
      this.initData()
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
